<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="js/vue.js"></script>
    <style>
            #box ul{
                list-style: none;
                width: 60%;
                margin: auto;
                border: 1px solid black;
                padding: 10px;
            }
            #box ul li{
                display: flex;
                justify-content: space-around;
                margin-top: 10px;
                border-bottom: 1px dashed #cccccc;
            }
            #box ul li:last-child{
                border-bottom: none;
            }
            #box ul li img{
                width: 100px;
            }
            .all{
                width: 60%;
                margin: auto;
            }
        </style>
</head>
<body>
    <div id="box">
        <div class="all" v-if="list.length === 0">购物车空空如也</div>
        <template v-else>
                <div class="all">
                    <!-- <input type="checkbox" v-model="isAll" @change="checkedAll">全选|全不选 -->
                    <input type="checkbox" v-model="compCheckAll">全选|全不选
                </div>
                <ul>
                    <li v-for="(good,index) in list" :key="good.id">
                        <div>
                            <input 
                                type="checkbox" 
                                v-model="checkedGood" 
                                :value="good"
                            > 
                        </div>
                        <div>
                           <img :src="good.pic" alt="">
                        </div>
                        <div>
                           <div>名称:{{good.name}}</div>
                           <div>价格:{{good.price}}</div>
                        </div>
                        <div>
                            <button @click="good.number--" :disabled="good.number === 1 ? true : false">-</button>
                            <span>{{good.number}}</span>
                            <button @click="good.number++" :disabled="good.number === good.limit ? true : false">+</button>
                        </div>
                        <div>
                            <button @click="handleDel(good.id)">删除</button>
                        </div>
                    </li>
                </ul>
                <div class="all">总金额: {{getTotalPrice()}}</div>
        </template>
    </div>
    <script>
        //模态窗案例 .self
        
        const vm = new Vue({
            el:"#box",
            data:{
                // isAll:false,
                checkedGood:[],
                list:[
                    {
                        name:"商品1",
                        price:10,
                        number:1,
                        id:1,
                        limit:5,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品2",
                        price:20,
                        number:5,
                        id:2,
                        limit:8,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品3",
                        price:30,
                        number:10,
                        id:3,
                        limit:15,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品4",
                        price:8,
                        number:6,
                        id:4,
                        limit:18,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品5",
                        price:14,
                        number:2,
                        id:5,
                        limit:7,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    }
                ]
            },
            methods:{
                // checkedAll(){
                //     // 判断是否选中了全选按钮
                //     if(this.isAll){
                //         // 将所有的good对象保存到checkedGood
                //         this.checkedGood = this.list
                //     }else{
                //         this.checkedGood = []
                //     }
                // },
                getTotalPrice(){
                    let sum = 0;
                    // 从checkedGood中获取总金额
                    this.checkedGood.forEach(good => {
                        sum += good.number * good.price
                    })
                    return sum;
                },
                // selectOne(){
                //     // 判断是否全部选中了
                //     if(this.checkedGood.length === this.list.length){
                //         this.isAll = true
                //     }else{
                //         this.isAll = false
                //     }
                // },
                handleDel(id){
                    // 根据id删除list中的商品
                    this.list = this.list.filter(good => good.id != id)
                    // 根据id删除选的checkedGood中的商品
                    this.checkedGood = this.checkedGood.filter(good => good.id != id)
                    // 判断是否全部命中
                    this.selectOne()
                }
            },
            computed:{
                compCheckAll:{
                    get(){
                        // 全部选中时true,只要有一个没有被选中为false
                        return this.checkedGood.length === this.list.length
                    },
                    set(flag){ // true|false
                        // console.log(flag);
                        if(flag){
                            // 将所有的good对象保存到checkedGood
                            this.checkedGood = this.list
                        }else{
                            this.checkedGood = []
                        }
                    }
                }
            }
        })

    </script>
</body>
</html>